import matplotlib.pyplot as plt
import numpy as np

# 读取数据文件
pred_data = []
true_data = []
with open('data_pre.txt', 'r', encoding='utf-8') as f:
    for line in f:
        if line.startswith('预测值:['):
            values = line.strip().split(': ')[1].strip()
            if values.startswith('[') and values.endswith(']'):
                values = values[1:-1]
            try:
                pred_data.append([float(x.strip()) for x in values.split(',') if x.strip()])
            except ValueError:
                cleaned_values = values.replace(' ', '').split(',')
                pred_data.append([float(x) for x in cleaned_values if x])
        elif line.startswith('真实值:['):
            values = line.strip().split(': ')[1].strip()
            if values.startswith('[') and values.endswith(']'):
                values = values[1:-1]
            try:
                true_data.append([float(x.strip()) for x in values.split(',') if x.strip()])
            except ValueError:
                cleaned_values = values.replace(' ', '').split(',')
                true_data.append([float(x) for x in cleaned_values if x])

# 转换为numpy数组
pred_data = np.array(pred_data)
true_data = np.array(true_data)

# 绘制图形
plt.figure(figsize=(10, 6))
plt.plot(pred_data[0], label='预测值', color='blue')
plt.plot(true_data[0], label='真实值', color='red')
plt.title('预测值与真实值对比')
plt.xlabel('样本索引')
plt.ylabel('值')
plt.legend()
plt.grid(True)
plt.show()